<template>
  <div class="dashboard-container">
    <div class="dashboard-header">
      <h1>欢迎来到后台管理系统</h1>
      <p>当前用户：{{ userInfo.nickname || userInfo.username }}</p>
    </div>

    <el-row :gutter="20" class="dashboard-cards">
      <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
        <el-card class="dashboard-card">
          <div class="card-content">
            <div class="card-icon user-icon">
              <el-icon><User /></el-icon>
            </div>
            <div class="card-info">
              <h3>1,234</h3>
              <p>用户总数</p>
            </div>
          </div>
        </el-card>
      </el-col>

      <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
        <el-card class="dashboard-card">
          <div class="card-content">
            <div class="card-icon order-icon">
              <el-icon><ShoppingCart /></el-icon>
            </div>
            <div class="card-info">
              <h3>5,678</h3>
              <p>订单总数</p>
            </div>
          </div>
        </el-card>
      </el-col>

      <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
        <el-card class="dashboard-card">
          <div class="card-content">
            <div class="card-icon revenue-icon">
              <el-icon><Money /></el-icon>
            </div>
            <div class="card-info">
              <h3>¥123,456</h3>
              <p>总收入</p>
            </div>
          </div>
        </el-card>
      </el-col>

      <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
        <el-card class="dashboard-card">
          <div class="card-content">
            <div class="card-icon visit-icon">
              <el-icon><View /></el-icon>
            </div>
            <div class="card-info">
              <h3>9,876</h3>
              <p>访问量</p>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="dashboard-charts">
      <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
        <el-card>
          <template #header>
            <span>最近访问</span>
          </template>
          <div class="chart-placeholder">
            <p>图表区域</p>
            <p>可以集成 ECharts 或其他图表库</p>
          </div>
        </el-card>
      </el-col>

      <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
        <el-card>
          <template #header>
            <span>系统信息</span>
          </template>
          <div class="system-info">
            <div class="info-item">
              <span class="label">操作系统：</span>
              <span class="value">{{ systemInfo.os }}</span>
            </div>
            <div class="info-item">
              <span class="label">浏览器：</span>
              <span class="value">{{ systemInfo.browser }}</span>
            </div>
            <div class="info-item">
              <span class="label">屏幕分辨率：</span>
              <span class="value">{{ systemInfo.screen }}</span>
            </div>
            <div class="info-item">
              <span class="label">当前时间：</span>
              <span class="value">{{ currentTime }}</span>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useUserStore } from '@/stores/user'

const userStore = useUserStore()
const userInfo = computed(() => userStore.userInfo)

const currentTime = ref('')
const systemInfo = ref({
  os: '',
  browser: '',
  screen: ''
})

let timer = null

const updateTime = () => {
  currentTime.value = new Date().toLocaleString()
}

const getSystemInfo = () => {
  const ua = navigator.userAgent
  
  // 检测操作系统
  if (ua.indexOf('Windows') > -1) {
    systemInfo.value.os = 'Windows'
  } else if (ua.indexOf('Mac') > -1) {
    systemInfo.value.os = 'macOS'
  } else if (ua.indexOf('Linux') > -1) {
    systemInfo.value.os = 'Linux'
  } else {
    systemInfo.value.os = '未知'
  }
  
  // 检测浏览器
  if (ua.indexOf('Chrome') > -1) {
    systemInfo.value.browser = 'Chrome'
  } else if (ua.indexOf('Firefox') > -1) {
    systemInfo.value.browser = 'Firefox'
  } else if (ua.indexOf('Safari') > -1) {
    systemInfo.value.browser = 'Safari'
  } else if (ua.indexOf('Edge') > -1) {
    systemInfo.value.browser = 'Edge'
  } else {
    systemInfo.value.browser = '未知'
  }
  
  // 屏幕分辨率
  systemInfo.value.screen = `${screen.width} x ${screen.height}`
}



onMounted(() => {
  updateTime()
  getSystemInfo()
  timer = setInterval(updateTime, 1000)
})

onUnmounted(() => {
  if (timer) {
    clearInterval(timer)
  }
})
</script>

<style lang="scss" scoped>
.dashboard-container {
  padding: 20px;
}

.dashboard-header {
  margin-bottom: 30px;
  text-align: center;
  
  h1 {
    color: #333;
    margin-bottom: 10px;
  }
  
  p {
    color: #666;
    font-size: 16px;
  }
}

.dashboard-cards {
  margin-bottom: 30px;
  
  .dashboard-card {
    margin-bottom: 20px;
    
    .card-content {
      display: flex;
      align-items: center;
      
      .card-icon {
        width: 60px;
        height: 60px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 20px;
        font-size: 24px;
        color: white;
        
        &.user-icon {
          background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        &.order-icon {
          background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
        }
        
        &.revenue-icon {
          background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
        }
        
        &.visit-icon {
          background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
        }
      }
      
      .card-info {
        h3 {
          margin: 0 0 5px 0;
          font-size: 24px;
          font-weight: bold;
          color: #333;
        }
        
        p {
          margin: 0;
          color: #666;
          font-size: 14px;
        }
      }
    }
  }
}

.dashboard-charts {
  .chart-placeholder {
    height: 300px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: #f5f5f5;
    border-radius: 4px;
    color: #999;
  }
  
  .system-info {
    .info-item {
      display: flex;
      justify-content: space-between;
      padding: 10px 0;
      border-bottom: 1px solid #f0f0f0;
      
      &:last-child {
        border-bottom: none;
      }
      
      .label {
        color: #666;
        font-weight: 500;
      }
      
      .value {
        color: #333;
      }
    }
  }
}

/* 移动端适配 */
@media (max-width: 768px) {
  .dashboard-container {
    padding: 10px;
  }
  
  .dashboard-cards {
    .dashboard-card {
      .card-content {
        .card-icon {
          width: 50px;
          height: 50px;
          font-size: 20px;
          margin-right: 15px;
        }
        
        .card-info {
          h3 {
            font-size: 20px;
          }
        }
      }
    }
  }
}

/* 移动端适配 */
@media (max-width: 768px) {
  .dashboard-container {
    padding: 10px;
  }

  .dashboard-header {
    margin-bottom: 20px;

    h1 {
      font-size: 20px;
      margin-bottom: 8px;
    }

    p {
      font-size: 14px;
    }
  }

  .dashboard-cards {
    margin-bottom: 20px;

    .dashboard-card {
      margin-bottom: 15px;

      .el-card__header {
        padding: 16px;

        h3 {
          font-size: 16px;
        }
      }

      .el-card__body {
        padding: 16px;
      }

      .card-content {
        .card-stats {
          grid-template-columns: repeat(2, 1fr);
          gap: 12px;

          .stat-item {
            padding: 12px;

            .stat-value {
              font-size: 18px;
            }

            .stat-label {
              font-size: 12px;
            }
          }
        }

        .card-info {
          h3 {
            font-size: 16px;
            margin-bottom: 12px;
          }

          .info-list {
            .info-item {
              font-size: 14px;
              margin-bottom: 8px;

              .info-label {
                min-width: 80px;
              }
            }
          }
        }
      }
    }
  }
}
</style>
